/*
 * FreeRTOS Kernel V10.4.1
 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * https://www.FreeRTOS.org
 * https://github.com/FreeRTOS
 *
 * 1 tab == 4 spaces!
 */

#include <SRV_UART/srv_uart.h>
#include "portmacro.h"
#include "FreeRtos.h"
#include "task.h"
#include "UART_Logging.h"
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include "IfxCpu.h"

#if 1
uint8  sw_lock = 0u;
void init_queue()
{
#if ANSYS_UART
    uint8 i = 0u;
    for(i = 0u;i<MAX_MESSAGE;++i)
    {
        memset(debug_info_fifo.msg[i].strings,0,MAXCHARS);
        debug_info_fifo.msg[i].len = 0u;
    }
    debug_info_fifo.front_cpu1 = 0u;
    debug_info_fifo.size_cpu0 = 0u;
    debug_info_fifo.rear_cpu0 = 0u;
#else
#if QZX_WATCH_TASK_SEQ
    uart_quque.l = 0u;
    uart_quque.r = 0u;
    uart_quque.size = 0u;
#endif
#endif
}

/*
 * 输入字符串格式，以及原始数据
 *
 * */
#if ANSYS_UART
void uart_print_async(uint8 len,char *s)
#else
#if QZX_WATCH_TASK_SEQ
void uart_print_async(uint8 task,uint32 tick)
#endif
#endif
#if ANSYS_UART
{
    if(debug_info_fifo.size_cpu0 >= MAX_MESSAGE)
    {
        sendUARTMessage("fifo overflow",14);
        while(1);
    }
    debug_info_fifo.msg[debug_info_fifo.rear_cpu0].len = len;
    memcpy(debug_info_fifo.msg[debug_info_fifo.rear_cpu0].strings,s,len);
    debug_info_fifo.rear_cpu0++;
    debug_info_fifo.rear_cpu0 = debug_info_fifo.rear_cpu0%MAX_MESSAGE;
    debug_info_fifo.size_cpu0++;
}
#else
#if QZX_WATCH_TASK_SEQ
    {

    if(uart_quque.size >= QUEUE_MAX_SIZE)
    {
        Ifx_Lwip_printf("losing data");
        return;
    }

    uint8 coreid = (uint8)IfxCpu_getCoreId();
    while(sw_lock);
    SET_BIT(sw_lock,coreid);
    uart_quque.r = (uart_quque.r++)%QUEUE_MAX_SIZE;
    uart_quque.tasks[uart_quque.r].task = task;
    uart_quque.tasks[uart_quque.r].tick = tick;
    uart_quque.size++;
    CLEAR_BIT(sw_lock,coreid);
    return;
    }
#endif
#endif

#if ANSYS_UART
void uart_print_task(void)
{
    if((debug_info_fifo.size_cpu0 == 0u))
    {
        return;
    }
    if((debug_info_fifo.front_cpu1 == debug_info_fifo.rear_cpu0 )&&debug_info_fifo.size_cpu0 <MAX_MESSAGE)
    {
        return;
    }
    Ansys_Msg_Type * pMsg = &(debug_info_fifo.msg[debug_info_fifo.front_cpu1]);
    sendUARTMessage(pMsg->strings,pMsg->len);
    debug_info_fifo.front_cpu1++;
    debug_info_fifo.front_cpu1 = debug_info_fifo.front_cpu1 % MAX_MESSAGE;
    debug_info_fifo.size_cpu0--;
    sendUARTMessage("\r\n", 2);
}
#else
#if QZX_WATCH_TASK_SEQ
    Ifx_SizeT cnt = 0;
    uint8 coreid = (uint8)IfxCpu_getCoreId();
    while(sw_lock);
    SET_BIT(sw_lock,coreid);
    uint8 task = uart_quque.tasks[uart_quque.l].task;
    uint32 time = uart_quque.tasks[uart_quque.l].tick;
    uart_quque.l = (uart_quque.l++)%MAXCHARS;
    uart_quque.size--;
    CLEAR_BIT(sw_lock,coreid);
    Ifx_Lwip_printf("%d-%d",task,time);
}
#endif
#endif


#endif



